var subjectCategoryType;
var id;
var groupid;
var iconid;
var subid;
var menuid;
var businessId;
var backstage_access_personnelstyle;
$(function() {
	groupid = ly.getUrlParam("groupid");
	iconid = ly.getUrlParam("iconid");
	subid = ly.getUrlParam("subid");
	menuid = ly.getUrlParam("menuid");

	backUserType = ly.getUrlParam("backUserType");
	backstage_access_personnelstyle = window.localStorage.getItem("backstage-access-personnelstyle");
	if(backstage_access_personnelstyle == "2") {
		$(".changeTeam").hide();
		$(".changeWork").hide();
		$("#userType").attr("disabled", "disabled");
	}
	id = ly.getUrlParam("id");
	if(backUserType == 2) {
		$(".body_title").html("编辑");
		$(".form").getForm({
			id: id,
			ajaxform: true
		}, function(data) {
			businessId = data.businessId;
			if(data.fileUpload!=null){
				$('.imgbox').find('img').attr('src', data.fileUpload.url)
			}if(data.userType == 2) {
				$("#changeBusinessId").removeClass("hide");
				selectbusinessList(businessId);
			}
			selectRoleList(data.roleId);
			$("#userType").getSelect({
				type: "radio",
				asName: "userType",
				list: [{
						value: "1",
						display: "管理员"
					},
					{
						value: "2",
						display: "部门经理"
					}
				],
				disabled: true,
				default: data.userType
			});
			$("#isUsing").getSelect({
				type: "radio",
				asName: "isUsing",
				list: [{
						value: "1",
						display: "启用"
					},
					{
						value: "0",
						display: "禁用"
					}
				],
				default: data.isUsing
			});
		});
	} else {
		selectRoleList("");
		$("#userType").getSelect({
			type: "radio",
			asName: "userType",
			list: [{
					value: "1",
					display: "管理员"
				},
				{
					value: "2",
					display: "部门经理"
				}
			],
			default: '1'
		});
		$("#isUsing").getSelect({
			type: "radio",
			asName: "isUsing",
			list: [{
					value: "1",
					display: "启用"
				},
				{
					value: "0",
					display: "禁用"
				}
			],
			default: '1'
		});
	}

	//获取到选择的值
	$("body").on("click", "input[name='userType']", function() {
		var userType = this.value;
		if(userType == '2') {
			$(".changeTeam").removeClass("hide");
			//$("#changeBusinessId").removeClass("hide");
			selectTeamList("");
			//selectbusinessList(businessId);
		} else {
			$(".changeTeam").addClass("hide");
			$(".changeWork").addClass("hide");
			$(".changeBusinessId").addClass("hide");
		}
	});

	//提交
	$("body").on("click", "#save-btn-commodityClassification", function(event) {
		event.preventDefault();

		var success = $('.form').validate('submitValidate');
		if(success) {
			if($.trim($("#roleId").val()) == "") {
				ly.toast("请选择角色");
				return false;
			} else {
				//验证通过提交表单
				submitFom();
			}
		}
	})

	//选择部门
	$("body").on("change", "#teamId", function() {

		selectWorkerList($(this).val());
		$(".changeWork").removeClass("hide");
	});

	//选择工种
	$("body").on("change", "#workerId", function() {
		selectbusinessList("", $(this).val(), $("#teamId").val());
		$(".changeBusinessId").removeClass("hide");
	});

	//批量删除
	$("#batchDelBtn").click(function() {
		var c = "";
		$("input[name=datachk]:checked").each(function() {
			c += $(this).val() + ',';
		});
		deleteFirstCategory(c);
	});
});

/*
 * 提交表单
 */
function submitFom() {
	var account = $("#account").val();
	//var password =md5($("#password").val());
	var nickName = $("#nickName").val();
	var userType = $("#userType input:checked").val();
	var isUsing = $("#isUsing input:checked").val();
	var businessId = $('select[name=businessId] option:checked').val();
	var roleId = $('select[name=roleId] option:checked').val();

	var fileName = $('#bPicUrl input').attr('filename');
	var filePath = $('#bPicUrl input').attr('value');
	var fileType = $('#bPicUrl input').attr('filetype');
	var fileSize = $('#bPicUrl input').attr('filesize');

	var obj = {};
	var url = site.gateUrl + "/back/backUser/insertOrUpdateBackUser";
	var f = { //图标
		'fileName': fileName,
		'filePath': filePath,
		//'fileType':fileType,
		'fileSize': fileSize
	};
	if(backUserType == 1) {
		//新增
		obj = {
			'account': account,
			//'password':password,
			'nickName': nickName,
			'userType': userType,
			//'businessId': businessId,
			'isUsing': isUsing,
			'roleId': roleId
		};
	} else {
		//修改
		var thisId = $("#id").val();
		obj = {
			'account': account,
			//'password':password,
			'nickName': nickName,
			'userType': userType,
			//'businessId': businessId,
			'roleId': roleId,
			'isUsing': isUsing,
			'id': thisId
		};
	}
	obj.fileUpload = f;
	console.log(obj);
	request({
		url: url,
		data: obj,
		type: "post",
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				if(backUserType == 1) {
					ly.alert('保存成功', function() {
						ly.goto("backUser.html");
					});
				} else {
					ly.alert('修改成功', function() {
						ly.goto("backUser.html");
					});
				}
			} else {
				ly.toast(f.desc);
			}
		}
	});

}

function selectbusinessList(businessId, workId, teamId) {
	var list = new Array();
	request({
		url: site.gateUrl + "/sUsers/selectUsersList",
		data: {
			'num': 1,
			'size': 1000,
			'isUsing': 1,
			"teamId": teamId,
			"workId": workId
		},
		type: "get",
		ajaxform: true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {
					value: ' ',
					display: '请选择'
				};
				list.push(obj);
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.nickName
						};
						list.push(obj);
					});
				}
				$("#businessId").empty();
				$("#businessId").getSelect({
					type: "select",
					asName: "businessId",
					list: list,
					default: businessId
				});
			}
		}
	});
}

function selectRoleList(roleId) {
	var list = new Array();
	request({
		url: site.apiUrl + "/role/selectAllRoleList",
		//data:{'num':1,'size':100},
		type: "post",
		ajaxform: true,
		callback: function(f) {
			if(f.code == "rest.success") {
				var obj = {
					value: ' ',
					display: '请选择'
				};
				list.push(obj);
				if(f.result != null && f.result != '') {
					$.each(f.result, function(i, o) {
						obj = {};
						obj = {
							value: o.id,
							display: o.roleName
						};
						list.push(obj);
					});
				}
				$("#roleId").getSelect({
					type: "select",
					asName: "roleId",
					list: list,
					default: roleId
				});
			}
		}
	});
}

//选择工种
function selectWorkerList(teamId) {
	var list = new Array();
	request({
		url: site.gateUrl + "/back/team/selectTeamWorkerListByTeamId",
		data: {
			'id': teamId
		},
		type: "get",
		ajaxform: true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {
					value: ' ',
					display: '请选择'
				};
				list.push(obj);
				if(f.result != null && f.result != '') {
					$.each(f.result, function(i, o) {
						obj = {};
						obj = {
							value: o.id,
							display: o.workerName
						};
						list.push(obj);
					});
				}
				$("#workerId").empty();
				$("#workerId").getSelect({
					type: "select",
					asName: "workerId",
					list: list,
					default: businessId
				});
			}
		}
	});
}

//选择部门
function selectTeamList(teamId) {
	var list = new Array();
	request({
		url: site.gateUrl + "/back/team/selectTeamList",
		data: {
			'num': 1,
			'size': 100,
			'isUsing': 1
		},
		type: "get",
		ajaxform: true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {
					value: ' ',
					display: '请选择'
				};
				list.push(obj);
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.teamName
						};
						list.push(obj);
					});
				}
				$("#teamId").empty();
				$("#teamId").getSelect({
					type: "select",
					asName: "teamId",
					list: list,
					default: teamId
				});
			}
		}
	});
}